﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.ManageProduct
{
    public partial class ManageSubCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            if (!string.IsNullOrEmpty(A.M.GetQueryString("Id")))
            {
                litCategoryParent.Text =
                    A.Controller.ProductCategoryController.GetProductCategoryById(
                        Convert.ToInt32(A.M.GetQueryString("Id"))).Name;
                grdCategory.DataSource =
                    A.Controller.ProductCategoryController.GetAllSubCategoryByParentId(A.M.GetQueryString("Id"));
            }

            grdCategory.DataBind();
        }

        protected void grdAccount_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductCategory productCategory = new ProductCategory();
                        TextBox txtName = (TextBox)e.Item.FindControl("txtName");
                        TextBox txtOrderDisplay = (TextBox)e.Item.FindControl("txtOrderDisplay");
                        productCategory.Name = txtName.Text.Trim();
                        productCategory.IsActive = true;
                        productCategory.ParentId = Convert.ToInt32(A.M.GetQueryString("Id"));
                        if (A.M.IsValidNumber(txtOrderDisplay.Text))
                            productCategory.OrderDisplay = Convert.ToInt32(txtOrderDisplay.Text);
                        else
                            productCategory.OrderDisplay = 100;
                        d.ProductCategories.InsertOnSubmit(productCategory);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdUpdate":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductCategory productCategory = d.ProductCategories.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        TextBox txtName = (TextBox)e.Item.FindControl("txtName");
                        TextBox txtOrderDisplay = (TextBox)e.Item.FindControl("txtOrderDisplay");
                        productCategory.Name = txtName.Text.Trim();
                        if (A.M.IsValidNumber(txtOrderDisplay.Text))
                            productCategory.OrderDisplay = Convert.ToInt32(txtOrderDisplay.Text);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdDelete":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductCategory productCategory = d.ProductCategories.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        d.ProductCategories.DeleteOnSubmit(productCategory);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdIsActive":
                    using (DALDataContext d = new DALDataContext())
                    {
                        ProductCategory productCategory = d.ProductCategories.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        productCategory.IsActive = !productCategory.IsActive;
                        d.SubmitChanges();
                    }
                    break;
            }
            BindData();
        }

        protected void grdCategory_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList cboParent = (DropDownList) e.Item.FindControl("cboParent");

            }
        }
    }
}